﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Services;

namespace ScsmIncidentLibrary
{
    [WebService(Namespace = "http://servicemanager.incidents.dallascowboys.net")]
    [ServiceBehavior(Namespace = "http://servicemanager.incidents.dallascowboys.net")]
    public class Incident : IIncident
    {
        private ConfigMgr _configMgr;
        public Incident() : this(new ConfigMgr())
        {
        }

        public Incident(ConfigMgr configMgr)
        {
            if (configMgr == null)
            {
                this._configMgr = new ConfigMgr();
            }
            else
            {
                this._configMgr = configMgr;
            }
            
                        
        }

        #region IIncident Members

        [WebMethod]
        public string Create(string userDomainNameForIncidentCreatedBy, string userNameForIncidentCreatedBy, string title, string description)
        {
            if (string.IsNullOrEmpty(userDomainNameForIncidentCreatedBy))
            {
                return "Parameter userDomainNameForIncidentCreatedBy must not be null or empty string.";
            }
            if (string.IsNullOrEmpty(userNameForIncidentCreatedBy))
            {
                return "Parameter userNameForIncidentCreatedBy must not be null or empty string.";
            }

            try
            {
                ServiceManagerGateway svcMgrGateway = new ServiceManagerGateway();
                svcMgrGateway.CreateIncident(userDomainNameForIncidentCreatedBy, userNameForIncidentCreatedBy, title, description);
            }
            catch (Exception exception)
            {
                return FormatExceptionMessage("creating an incident", exception);
            }

            return string.Empty;
        }


        [WebMethod]
        public string Close(string currentUserDomainName, string currentUserName, string incidentId)
        {
            try
            {
                ServiceManagerGateway svcMgrGateway = new ServiceManagerGateway();
                svcMgrGateway.CloseIncidentByAffectedUser(currentUserDomainName, currentUserName, incidentId);
            }
            catch (Exception exception)
            {
                return FormatExceptionMessage("closing an incident", exception);
            }
            return string.Empty;
        }

        [WebMethod]
        public string Activate(string currentUserDomainName, string currentUserName, string incidentId)
        {
            try
            {
                ServiceManagerGateway svcMgrGateway = new ServiceManagerGateway();
                svcMgrGateway.ActivateIncidentByAffectedUser(currentUserDomainName, currentUserName, incidentId);
            }
            catch (Exception exception)
            {
                return FormatExceptionMessage("closing an incident", exception);
            }
            return string.Empty;
        }

        [WebMethod]
        public string Help()
        {
            string configHelp = _configMgr.HelpMessage();
            if(string.IsNullOrEmpty(configHelp))
            {
            return string.Format(@"
Create method returns a string representing any error that may occur and will be empty string if no error occurs.
    The userDomainNameForIncidentCreatedBy parameter must not be null and is the name of the domain for the user found in configuration items in Service Manager which will be used for the incident CreatedBy property.
    The userNameForIncidentCreatedBy parameter must not be null and is the name of the user found in configuration items in Service Manager which will be used for the incident CreatedBy property.
    The title parameter must not be null and will be used as the incident title.
    The description parameter must not be null and will be used as the incident title.      

Close method returns a string representing any error that may occur and will be empty string if no error occurs. The incident will not be closed unless the current user is the affected user.
    The currentUserDomainName parameter must not be null and is the name of the domain for the current user.
    The currentUserName parameter must not be null and is the login or user name for the current user.
    The incidentId is the Id of the incident to be closed.      

Activate method returns a string representing any error that may occur and will be empty string if no error occurs. The incident will be activateded if not closed and the current user is the affected user.
    The currentUserDomainName parameter must not be null and is the name of the domain for the current user.
    The currentUserName parameter must not be null and is the login or user name for the current user.
    The incidentId is the Id of the incident to be activated.    
");
            }
            else
            {
                return configHelp;
            }
        }

        #endregion

        private string FormatExceptionMessage(string actionMessage, Exception exception)
        {
            return string.Format("An error occurred while " + actionMessage + ". Error message {0}", exception.ToString());
        }
    }
}
